package com.cloud.sso.web;

import com.cloud.sso.common.constant.OperateType;
import com.cloud.sso.common.exception.AppException;
import com.cloud.sso.common.log.annoatation.BusinessLog;
import com.cloud.sso.common.page.AppPageInfo;
import com.cloud.sso.common.result.Result;
import com.cloud.sso.dto.AppQueryDto;
import com.cloud.sso.dto.AppSaveDto;
import com.cloud.sso.entity.Application;
import com.cloud.sso.service.ApplicationService;
import com.cloud.sso.vo.ApplicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 应用处理类
 */
@RestController
@RequestMapping("/api/app")
public class AppController {

    @Autowired
    private ApplicationService applicationService;

    /**
     * 分页查询应用 *
     *
     * @param queryDto
     * @return
     */
    @RequestMapping("/query")
    public Result<AppPageInfo> queryByPage(@RequestBody AppQueryDto queryDto) {
        return Result.success(applicationService.queryByPage(queryDto));
    }

    /**
     * 查询所有应用
     *
     * @return
     */
    @RequestMapping("/all")
    public Result queryByPage() {
        return Result.success(applicationService.all());
    }

    /**
     * 保存应用 *
     *
     * @param appSaveDto
     * @param result
     * @return
     */
    @BusinessLog(value = "新增应用", module = "应用管理", field = "appName", type = OperateType.INSERT)
    @PostMapping("/save")
    public Result save(@RequestBody @Validated AppSaveDto appSaveDto,
                       BindingResult result) {
        if (result.hasErrors()) {
            throw new AppException(result.getFieldError().getDefaultMessage());
        }

        applicationService.saveApplication(appSaveDto);
        return Result.success(1);
    }

    /**
     * 修改应用信息 *
     *
     * @param appSaveDto
     * @param result
     * @return
     */
    @BusinessLog(value = "更新应用", module = "应用管理", field = "appName", type = OperateType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody @Validated AppSaveDto appSaveDto,
                         BindingResult result) {
        if (result.hasErrors()) {
            throw new AppException(result.getFieldError().getDefaultMessage());
        }

        applicationService.saveApplication(appSaveDto);
        return Result.success(1);
    }

    /**
     * 获取应用详情  *
     *
     * @param id
     * @return
     */
    @RequestMapping("/get")
    public Result<ApplicationVo> get(@RequestParam String id) {
        return Result.success(applicationService.get(id));
    }

    /**
     * 删除应用信息 *
     *
     * @param id
     * @return
     */
    @BusinessLog(value = "删除应用", module = "应用管理", field = "id", type = OperateType.DELETE)
    @RequestMapping("/delete")
    public Result delete(@RequestParam String id) {
        applicationService.deleteApplication(id);
        return Result.success(1);
    }

}
